/* eslint-disable max-classes-per-file */
/* eslint-disable no-unused-vars */
/* eslint-disable no-param-reassign  */
/* eslint-disable import/no-cycle */
import ControlUtil from '../controls/ControlUtil';
import DesignUtil from './DesignUtil';

/**
 * 工具栏拖拽效果封装
 */
class DragData {
  constructor(toolbar, data) {
    this.toolbar = toolbar;
    this.data = data;
  }

  // 绑定开始拖拽事件
  bindDrapStartEventProps(isDraggable) {
    return {
      draggable: isDraggable === false ? 'false' : 'true',
      onDragStart: (e) => {
        const dragData = this.toolbar.dragData;
        if (dragData.data == null) {
          dragData.data = this.data;
          dragData.data.startPoint = { x: e.pageX, y: e.pageY };
          if (dragData.data.key == null) {
            e.dataTransfer.effectAllowed = 'copy';
          } else {
            e.dataTransfer.effectAllowed = 'move';
          }
        }
        // return e.preventDefault();
      },
      onDragEnd: (e) => {
        this.toolbar.dragData = new DragData(this.toolbar, null);
        // return e.preventDefault();
      },
    };
  }
}

class ToolDrap {
  constructor() {
    this.dragData = new DragData(this, null);
  }

  // 绑定结束拖拽事件
  bindDropEndEventProps(parentForm) {
    return {
      onDrop: (e) => {
        const dragData = this.dragData;
        if (dragData.data != null) {
          const point = { x: e.pageX, y: e.pageY };
          const fromdata = dragData.data;
          this.dragData.data = null;
          ControlUtil.movePointControl(parentForm.controls, point, fromdata, parentForm);
        }
      },
      onDragOver: (e) => {
        return e.preventDefault();
      },
    };
  }
}

export default { DragData, ToolDrap };
